﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using HYElectricCommon;

namespace YArchitech.MEP.Electric.Common
{
	public class DataBaseTempFileOperate
	{
		public static string GetDBVersion(string fileName)
		{
			string result = "1.0";
			List<double> list = new List<double>();
			string connStr = "Data Source=" + fileName + ";Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10";
			try
			{
				SqLiteHelper.SqlLiteDatabase(connStr);
			}
			catch (Exception)
			{
				throw new Exception("读取数据库文件错误");
			}
			bool flag = false;
			DataTable dataTable = Database.ExecQuery("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name");
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				if (dataTable.Rows[i][0].ToString() == "Version")
				{
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				return result;
			}
			DataTable dataTable2 = Database.ExecQuery("select Version from [Version]");
			for (int j = 0; j < dataTable2.Rows.Count; j++)
			{
				if (!Convert.IsDBNull(dataTable2.Rows[j]["Version"]))
				{
					list.Add(Convert.ToDouble(dataTable2.Rows[j]["Version"]));
				}
			}
			if (list.Count > 0)
			{
				result = list.Max().ToString();
			}
			return result;
		}

		public static bool IsSameVersion(string fileName, string otherFileName)
		{
			bool result = false;
			string dbversion = DataBaseTempFileOperate.GetDBVersion(fileName);
			string dbversion2 = DataBaseTempFileOperate.GetDBVersion(otherFileName);
			if (Convert.ToDouble(dbversion) == Convert.ToDouble(dbversion2))
			{
				result = true;
			}
			return result;
		}

		public static void CopyDBFile(string fileName, string toPath)
		{
			DataBaseTempFileOperate.GetDBVersion(fileName);
			int num = fileName.LastIndexOf("\\");
			string destFileName = toPath + "\\" + fileName.Substring(num + 1);
			File.Copy(fileName, destFileName, true);
		}

		public static bool IsExistFile(string fileName)
		{
			return File.Exists(fileName);
		}
	}
}
